// pages/chooseContent/chooseContent.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sonList: [],
    index: 0,
    id: 0,
    name: [],
    indexList: [],
    exist: false,
  },
  request: function() {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/service/cate/findAll',
      // url: 'http://192.168.43.244:8002/service/cate/findAll',
      method: 'post',
      success: function(res) {
        console.log(res.data.data);
        console.log(res.data.data.sonList[that.data.index].sonList[that.data.id].sonList);
        that.setData({
          sonList: res.data.data.sonList[that.data.index].sonList[that.data.id].sonList,
          list: res.data.data,
          nameList: res.data.data.sonList[that.data.index].sonList
        })
        console.log(that.data.nameList);
        var name = []


        console.log(that.data.index)
        that.setData({
          name: that.data.nameList[that.data.id]
        });
        for (var i = 0; i < that.data.sonList.length; i++) {
          that.data.sonList[i].exist = false;
          that.setData({
            sonList: that.data.sonList
          })
          if (that.data.sonList[i].prop2List.length > 0) {
            that.data.sonList[i].exist = true;
            that.setData({
              sonList: that.data.sonList
            })
          };

        }



        console.log(that.data.sonList);
        var list = [];
        var arr=[];
        for (var i = 0; i < that.data.sonList.length; i++) {
          if (that.data.sonList[i].exist == true) {
            list.push(that.data.sonList[i])
          }else{
            arr.push(that.data.sonList[i])
          }
        }
        console.log(list);
        console.log(arr);
        var newArr=list.concat(arr);
        console.log(newArr);
        that.setData({
          sonList:newArr
        })
        // var arr = [];
        // arr = list;
        // for (var h = 0; h < that.data.sonList.length; h++) {
        //   for (var j = 0; j < arr.length; j++) {
        //     if(that.data.sonList[h]==arr[j]){
        //       // continue;
        //     }else{
        //       list.push(that.data.sonList);
        //     }
        //   }
        // }
        // console.log(list);
        // that.setData({
        //   sonList:list
        // })

        console.log(that.data.name)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  // 跳转到子类
  son: function(e) {
    var cateId = e.currentTarget.dataset.cateid;
    var site = e.currentTarget.dataset.site;
    var that = this;
    this.setData({
      site: site
    })
    // for (var i = 0; i < that.data.sonList.length; i++) {
    if (that.data.sonList[site].prop2List.length == 0) {
      wx.showModal({
        title: '提示',
        content: '当前服务暂未开放',
        showCancel: false
      })
    } else {
      wx.navigateTo({
        // index 跟id弄反了
        url: '../chooseDetail/chooseDetail?index=' + that.data.id + "&id=" + that.data.index + "&site=" + that.data.site + "&cateId=" + cateId + "&title=" + that.data.title
      })
    }
    // }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    console.log(options.index);
    console.log(options.cateId);
    // var index = JSON.parse(options.index);
    this.setData({
      index: options.index,
      id: options.id,

    })
    var that = this;
    console.log(options);
    if (options.title == "" || options.title == undefined) {
      that.setData({
        title: 0
      })
    } else {
      that.setData({
        title: options.title
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.request();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})